package com.mall.bargain.listener;

import com.mall.bargain.mapper.UserBargainMapper;
import com.mall.bargain.service.UserBargainService;
import com.mall.constant.strings.ConstantStrings;
import com.mall.entity.bargain.BgUserBargain;
import com.mall.utils.MatcherUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @author: W
 * @date: 2021/07/22/16:57
 * Other:
 */
@Component
public class RedisKeyOverTimelistener extends KeyExpirationEventMessageListener {

    @Autowired
    private UserBargainMapper userBargainMapper;

    public RedisKeyOverTimelistener(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }

    @Override
    public void onMessage(Message message, byte[] pattern) {
        if (!MatcherUtil.match(ConstantStrings.BG_REDIS_KEY,message.toString())){
            return;
        }

        String[] split = message.toString().split("_");

        String id = split[1];
        int userBargainId = Integer.parseInt(id);

        BgUserBargain userBargain = userBargainMapper.findOne(userBargainId);

        userBargain.setUpdateTime(new Date());
        userBargain.setStatus("2");
        userBargainMapper.update(userBargain);

    }
}
